﻿#region Copyright (c) 2000-2008 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{       AgDataGrid                                 }
{                                                                   }
{       Copyright (c) 2000-2008 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2008 Developer Express Inc.

using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
namespace DevExpress.AgDataGrid.Data {
	public enum CustomSummaryProcess { Start, Calculate, Finalize }
	public enum ColumnSortOrder { None = 0, Ascending, Descending };
	public enum SummaryItemType { Sum, Min, Max, Count, Average, Custom, None };	
	public class SummaryItem {
		DataColumn column;
		SummaryItemType summaryType;
		public SummaryItem(DataColumn column, SummaryItemType summaryType) {
			this.column = column;
			this.summaryType = summaryType;
		}
		public DataColumn Column { 
			get { return column; } 
			set {
				if(column == value) return;
				column = value; 
				Changed();
			} 
		}
		public SummaryItemType SummaryType { 
			get { return summaryType; } 
			set {
				if(summaryType == value) return;
				summaryType = value;
				Changed();
			} 
		}
		public object SummaryValue { get; set; }
		internal SummaryItemCollection Collection { get; set; }
		public object Tag { get; set; }
		public object Key { get { return Tag ?? this; } }
		protected void Changed() {
			if(Collection != null)
				Collection.SummaryItemChanged();
		}
		public bool AllowCalculate {
			get { 
				return !IsNoneSummary && (Column != null || SummaryType == SummaryItemType.Count || SummaryType == SummaryItemType.Custom); 
			}
		}
		public bool IsNoneSummary { get { return SummaryType == SummaryItemType.None; } }
		public bool IsCustomSummary { get { return SummaryType == SummaryItemType.Custom; } }
		public bool RequireValueConvert { get { return SummaryType == SummaryItemType.Average || SummaryType == SummaryItemType.Sum; } }
		public object ConvertValue(object val, out decimal numericVal) {
			numericVal = 0;
			if(!RequireValueConvert) return val;
			try {
				if(val != null && !object.ReferenceEquals(val, DBNull.Value))
					numericVal = Convert.ToDecimal(val);
			} catch {
				numericVal = 0;
			}
			return numericVal;
		}
	}
	public class SummaryItemCollection : ObservableCollection<SummaryItem> {
		internal void SummaryItemChanged() {
			OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
		}
		protected override void InsertItem(int index, SummaryItem item) {
			item.Collection = this;
			base.InsertItem(index, item);			
		}
		protected override void RemoveItem(int index) {
			this[index].Collection = null;
			base.RemoveItem(index);
		}
		protected override void ClearItems() {
			foreach(SummaryItem item in this)
				item.Collection = null;
			base.ClearItems();
		}
	}	
}
